<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_release.php');
class releaseControl
{
	var $release_m;
	function __construct()
	{
	
		$this->release_m = new releaseModule();
	}
	/**
	* 根据id得到release信息
	*
	* @param   int         $release_id
	*
	* @return  array
	*/
	function  get_release_id_info($release_id)
	{
		$sql = "SELECT * FROM `release` WHERE `id` = '$release_id'";
		return $this->release_m->get_release_info($sql);
	}
	
	/**
	* 增加或者修改pub_cdn_file
	* @param   string      $web_name      网站名称
	* @return  
	*/
	
	function pub_cdn_file($web_name ) {
		
		$file_name = '/var/www/website/cms.wondershare.com.br/httpdocs/image2/test.txt';
		system('/usr/local/worksh/CCURequest.pl --user admin@wondershare.com --pwd YYtuANCedeq1 --file '.$file_name.' --domain production --email admin@wondershare.com --action invalidate');
		
//		//调用Cdn接口
//		$sql = "SELECT release_time FROM  `release`  WHERE web = '{$web_name}' ORDER BY  release_time DESC  LIMIT 1";
//	
//		$last_pub_time = $GLOBALS['db']->getOne($sql);
//		
//		$last_pub_time = $last_pub_time + 24 * 3600;//得到最后发布网站的前一天时间 
//		$cur_time = time() ;
//		//当前时间大于最后发布网站的前一天时间 
//		if($cur_time > $last_pub_time ) {
//			
//			//发布最后发布网站的前一天时间 到当前日期的文件
//			$cur_time += 24 * 3600 ;
//			for ($time = $last_pub_time ; $time <= $cur_time; $time = $time + (24 * 3600)) {
//				
//				$file_name = CDN_URL.$web_name.'/'.date('Y', $time).'/'.date('m', $time).'/'.date('d', $time).'/urls.txt';
//				
//				if(file_exists($file_name))
//				{
//				
//					system('/usr/local/worksh/CCURequest.pl --user admin@wondershare.com --pwd YYtuANCedeq1 --file '.$file_name.' --domain production --email admin@wondershare.com --action invalidate');
//				}
//			}
//			
//			
//		} else {
//			
//			$file_name = CDN_URL.$web_name.'/'.date(Y).'/'.date(m).'/'.date(d).'/urls.txt';
//		
//			if(file_exists($file_name))
//			{
//				
//				system('/usr/local/worksh/CCURequest.pl --user admin@wondershare.com --pwd YYtuANCedeq1 --file '.$file_name.' --domain production --email admin@wondershare.com --action invalidate');
//			}
//		}
	}
	
	/**
	* 增加或者修改release
	*
	* @param   string      $type            操作类型
	* @param   int         $release_id      id
	* @param   string      $array           表单数组
	* @return  int
	*/
	
	function operate_release($type,$release_id,$array = '')
	{
		//$contents = urlencode($array['contents']);
	
		$_SESSION['contents'] = trim($array['contents']);

		echo "<script>location.href='http://74.86.224.106/1234gfwaef/sitersync.php?web={$array['web']}&backup={$array['backup']}&web_type={$array['web_type']}'</script>";
		exit;
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @param   int        $release_type 搜索字段类型（title还是内容）
	* @param   string     $keyword    搜索关键字
	* @return  array
	*/
	function get_url_info($page,$type,$url='',$web='')
	{
	
		if($type == 1)
		{
			if($url)
			{
				$sql_p = "SELECT COUNT(*) AS total FROM `release` WHERE `web`='{$url}'";
			}
			else{
				$sql_p = "SELECT COUNT(a.id) AS total FROM (SELECT `id` FROM `release` GROUP BY `web`) a ";
			}
		}
		else
		{
			$where_str = '';
			if(!empty($web))
			{
				$where_str.=" AND `web` = '".$web."' ";
			}
			if($url)
			{
				$sql_p = "SELECT COUNT(*) AS total FROM `release` WHERE `web`='{$url}' $where_str";
			}
			else{
				$sql_p = "SELECT COUNT(a.id) AS total FROM (SELECT `id` FROM `release` WHERE 1=1 $where_str  GROUP BY `web`) a ";
			}
		}
		
		$page_info = get_page_info($sql_p,20,$page);
		$page_url = get_page_url($page,$page_info['page_num']);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表release信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   int        $release_type 搜索字段类型（title还是内容）
	* @param   string     $keyword    搜索关键字
	* @return  array
	*/
	function  get_release_info($type,$start = 0,$pagesize = 10,$url='',$web='')
	{
		if($type == 'list')
		{
			if($url) {
				$sql = "
				SELECT 
				`id`,`web`,`contents`,`user`,`status`,`copyurl`,
				IF(`release_time` > 0, FROM_UNIXTIME(`release_time`), '') `release_time`
				FROM 
				`release`
				WHERE `web`='{$url}'
				ORDER BY `id` DESC LIMIT $start,$pagesize ";
			}
			else{
				$sql = "
				SELECT a.* FROM (SELECT 
				`id`,`web`,`contents`,`user`,`status`,`copyurl`,
				IF(`release_time` > 0, FROM_UNIXTIME(`release_time`), '') `release_time`
				FROM 
				`release` 
				ORDER BY `id` DESC) a GROUP BY a.web  LIMIT $start,$pagesize ";
			}
		}
		elseif($type == 'list_s')
		{
			$where_str = '';
			if(!empty($web))
			{
				$where_str.=" AND `web` = '".$web."' ";
			}
			if($url){
				$sql = "
				SELECT 
				`id`,`web`,`contents`,`user`,`status`,`copyurl`,
				IF(`release_time` > 0, FROM_UNIXTIME(`release_time`), '') `release_time`
				FROM `release` 
				WHERE `web`='{$url}' $where_str
				ORDER BY `id` DESC LIMIT $start,$pagesize ";
			}else{
				$sql = "
				SELECT a.* FROM (SELECT 
				`id`,`web`,`contents`,`user`,`status`,`copyurl`,
				IF(`release_time` > 0, FROM_UNIXTIME(`release_time`), '') `release_time`
				FROM 
				`release` 
				WHERE 1=1 $where_str
				ORDER BY `id` DESC) a GROUP BY a.web  LIMIT $start,$pagesize ";		    
			}
		}
		return $this->release_m->get_release_info($sql);
	}
	/**
	* 发布站点信息
	*
	* @param   string     $domain     发布站点
	* @return  int
	*/  
	/*   function release_sites($domain)
	{
	require('../rsync.php');
	$state = rsync($domain);
	return $state ? $state : 0;
	}*/
	/**
	* 备份数据库
	*
	* @return  int
	*/  
	function release_backup()
	{
		echo "<script>location.href='http://74.86.224.106/1234gfwaef/backup_data.php'</script>";
		exit;
	}
	
	function rsync_c()
	{
		$array = array
		(
		'web' => trim($_REQUEST['web']),
		'contents' => $_SESSION['contents'],
		'state' => trim($_REQUEST['state']),
		'type' => trim($_REQUEST['type']),
		'username' => $_SESSION[AName]
		);
		
		unset($_SESSION['contents']);
		
		if($array['type'] == 'add' && $array['username'])
		{
			if(!$array['state'] || $array['state']==1){
				return '发布不成功!!!!,请检查站点是否存在';
			}
			if($array['state']==3){
				return $array['web'] . " 同步发布出错,请联系信息中心!";
			} 
			
			
			$sql ="
			INSERT INTO 
			`release` 
			(
			`web`,
			`contents`,
			`release_time`,
			`user`,
			`status`
			) 
			VALUES (
			'$array[web]',
			'$array[contents]',
			UNIX_TIMESTAMP(),
			'$_SESSION[AName]',
			1
			) ";
			
			return $this->release_m->change_info($sql);
		}
		return '发布过程出错，请联系信息中心';
	}


}
?>